<!-- 消息 -->
<template>
	<view class="b-m-bg">
		<u-navbar :safeAreaInsetTop="true" title="消息" bgColor="transparent">
			<view slot="left" class="u-flex u-font-28">
				<view class="u-p-r-20" @click="getNew('')">刷新</view>
				<view @click="show = true">删除</view>
			</view>
		</u-navbar>
		<view class="p-card">
			<view class="heighly">
				<view v-if="newsList.length" class="u-p-30">
					<view
						@click="nextNav('/pages/news/xiaoxi?id=' + item.taInfo.id)"
						class="u-flex u-flex-items-center u-p-t-20 u-p-b-20 u-border-bottom"
						v-for="(item, index) in newsList"
						:key="index"
					>
						<u-avatar shape="circle" size="55" :src="item.taInfo.avatar"></u-avatar>
						<view class="u-flex-1 u-m-l-20">
							<view class="u-flex u-flex-between u-flex-items-center">
								<text class="u-font-32">{{ item.taInfo ? item.taInfo.name || item.taInfo.nickname : '' }}</text>
								<view class="m-c-h">{{ item.latestMessage ? get_time(item.latestMessage.sentTime + '') : '' }}</view>
							</view>
							<view class="u-flex u-flex-between u-flex-items-center u-m-t-20">
								<view v-if="item.latestMessage.content.content">
									<text class="u-font-29 m-c-h" v-if="item.latestMessage.messageType === 'RC:TxtMsg'">{{ item.latestMessage.content.content }}</text>
									<text class="u-font-29 m-c-h" v-if="item.latestMessage.messageType === 'RC:ImgMsg'">[图片]</text>
									<text class="u-font-29 m-c-h" v-if="item.latestMessage.messageType === 'RC:LBSMsg'">[地图]</text>
									<text class="u-font-29 m-c-h" v-if="item.latestMessage.messageType === 'RC:HQVCMsg'">[消息]</text>
								</view>
								<view v-else class="m-c-h u-font-29">消息已撤回</view>
								<view class="xiazai m-c-w" v-if="item.unreadMessageCount">{{ item.unreadMessageCount }}</view>
							</view>
						</view>
					</view>
				</view>
				<list-empty v-else text="暂时没有数据哟!" list="data" :listHighly="highly"></list-empty>
			</view>
			<u-back-top :scrollTop="pagescrollTop" top="200"></u-back-top>
		</view>
		<obtain :show="obtain" @showchange="obtainOk"></obtain>
		<u-loading-page loading-text="内当家APP" :loading="pageLoad"></u-loading-page>
		<u-modal :show="show" :content="content" @confirm="deleteAll" @cancel="show = false" :showCancelButton="true"></u-modal>
	</view>
</template>

<script>
export default {
	data() {
		return {
			show: false,
			content: '确定要删除全部记录？',
			newsList: []
		};
	},
	onShow() {
		let that = this;
		this.getNew();
		this.$ry.watch({
			// 监听会话列表变更事件
			conversation(event) {
				// 发生变更的会话列表
				const updatedConversationList = event.updatedConversationList;
				that.getNew(updatedConversationList);
			}
		});
	},
	methods: {
		obtainOk() {
			this.obtain = false;
			this.getNew();
		},
		deleteAll() {
			const conversation = this.$ry.Conversation.getList({
				count: 1000,
				startTime: 0,
				order: 0
			}).then(async conversationList => {
				for (var i = 0; i < conversationList.length; i++) {
					console.log(conversationList[i]);
					let conversation1 = this.$ry.Conversation.get({
						targetId: conversationList[i].targetId,
						type: 1
					});
					conversation1.destory().then(() => uni.removeStorageSync('xinxiaoxi'));
				}
				this.newsList = [];
			});
			this.show = false;
		},
		getNew(newList) {
			let that = this;
			if (uni.getStorageSync('rong_token')) {
				const conversation = this.$ry.Conversation.getList({
					count: 1000,
					startTime: 0,
					order: 0
				}).then(async conversationList => {
					if (newList) {
						let you = conversationList.some(news => {
							return news.targetId == newList[0].targetId;
						});
						if (you) {
							conversationList.map((news, nid) => {
								newList.map((list, lid) => {
									if (news.targetId == list.targetId) {
										conversationList[nid] = list;
									}
								});
							});
						} else {
							conversationList = conversationList.concat(newList);
						}
					}
					for (var i = 0; i < conversationList.length; i++) {
						let taInfo = await this.require('getiduser', { senderUserId: conversationList[i].targetId });
						if (taInfo) {
							conversationList[i].taInfo = taInfo;
						}
					}
					this.newsList = conversationList;
					this.pageLoad = false;
				});
				this.xinxiao();
			} else {
				this.pageLoad = false;
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.b-m-bg {
	background-color: #dbb07b;
}

.p-card {
	width: 100%;
	min-height: 100vh;
	border-top-left-radius: 25rpx;
	border-top-right-radius: 25rpx;
	background-color: #fff;
}
.xiazai {
	width: 45rpx;
	height: 45rpx;
	border-radius: 50%;
	background-color: #dbb07b;
	line-height: 40rpx;
	text-align: center;
}
</style>
